#include "pch.h"
#include "BaseTransform.h"
#include "DataQuery.h"

namespace gaeaway_load
{
    
    namespace datatransform
    {
        //std::string wstringTOstring(std::wstring wstr)
        //{
        //    std::string result;
        //    int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), static_cast<int>(wstr.size()), NULL, 0, NULL, NULL);
        //    if (len <= 0)return result;
        //    char* buffer = new char[len + 1];
        //    if (buffer == NULL)return result;
        //    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), static_cast<int>(wstr.size()), buffer, len, NULL, NULL);
        //    buffer[len] = '\0';
        //    result.append(buffer);
        //    delete[] buffer;
        //    return result;
        //}
        //std::wstring stringTOwstring(std::string str)
        //{
        //    std::wstring result;
        //    int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), static_cast<int>(str.size()), NULL, 0);
        //    if (len < 0)return result;
        //    wchar_t* buffer = new wchar_t[len + 1];
        //    if (buffer == NULL)return result;
        //    MultiByteToWideChar(CP_ACP, 0, str.c_str(), static_cast<int>(str.size()), buffer, len);
        //    buffer[len] = '\0';
        //    result.append(buffer);
        //    delete[] buffer;
        //    return result;
        //}

        gaeaway_load::EquipmentsOfOutput transformEquipmentList(const LoadedAirplanes& airplanes)
        {
            EquipmentsOfOutput result;
            for (auto air : airplanes)
            {
                for (auto equipment  : air.loadedEquipments)
                {
                    result.emplace_back(EquipmentOfOutput{
						equipment.equipmentID, 
						air.loadedIndex,
						air.typeID,
						equipment.leftYOfequipment });
                }
            }
            return result;
        }

		gaeaway_load::EquipmentsOfOutput transformEquipmentList(const LoadedAirplane& loadedAirplane)
		{
			EquipmentsOfOutput result;
			for (auto equipment : loadedAirplane.loadedEquipments)
			{
				result.emplace_back(EquipmentOfOutput{
					equipment.equipmentID,
					loadedAirplane.loadedIndex,
					loadedAirplane.typeID, 
					equipment.leftYOfequipment });
			}
			return result;
		}

		gaeaway_load::EquipmentsOfOutput transformEquipmentList(const LoadingAirplanes& airplanes)
		{
			EquipmentsOfOutput result(dataquery::getCountOfLoadedEquipments(airplanes));
			EquipmentsOfOutput::iterator iter = result.begin();
			for (auto air : airplanes)
			{
				for (auto equipment : air.loadedEquipments)
				{
					iter->airplaneKindID = air.typeID;
					iter->loadedAirplanIndex = air.loadedIndex;

					iter->equipmentID = equipment.equipmentID;
					iter->leftY = equipment.leftYOfequipment;

					iter++;
				}
			}
			std::sort(std::begin(result), std::end(result),
				[](EquipmentOfOutput e1, EquipmentOfOutput e2) {return e1.equipmentID < e2.equipmentID; });
			return result;
		}

		gaeaway_load::LoadedAirplane transformLoadedAirplane(const LoadingAirplane& airplane)
		{
			return static_cast<LoadedAirplane>(airplane);
		}

		gaeaway_load::LoadedAirplanes transformLoadedAirplanes(const LoadingAirplanes& airplanes)
		{
			gaeaway_load::LoadedAirplanes results(airplanes.size());
			std::transform(airplanes.begin(), airplanes.end(), results.begin(),
				[](LoadingAirplane air) {return datatransform::transformLoadedAirplane(air); });
			return results;
		}

		gaeaway_load::LoadingAirplane transformAirplaneToLoadingAirplane(const Airplane& curAirplane)
		{
			LoadingAirplane loadAirplane;

			loadAirplane.height = curAirplane.height;
			loadAirplane.length = curAirplane.length;
			loadAirplane.width = curAirplane.width;

			loadAirplane.typeID = curAirplane.typeID;
			loadAirplane.gravity = curAirplane.gravity;
			loadAirplane.minY = curAirplane.minY;
			loadAirplane.maxY = curAirplane.maxY;
			
			loadAirplane.weight = curAirplane.weight;
			loadAirplane.maxLoadWeight = curAirplane.maxLoadWeight;
			loadAirplane.locationTypeOfStartLoading = curAirplane.locationTypeOfStartLoading;
			loadAirplane.weightLimitList = curAirplane.weightLimitList;
			loadAirplane.tag = curAirplane.tag;

			//loadAirplane.loadedIndex = -1;
			//loadAirplane.currentFreeArea.min = 0;
			//loadAirplane.currentFreeArea.max = curAirplane.length;
			return loadAirplane;
		}

	}
    
}